package com.library.common.widget

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import com.library.common.R
import com.library.common.databinding.PopupChooseSideBinding
import com.library.common.util.DensityUtil


class VersionDialog(
    context: Context,
    content: String,
    newVersion: String,
    val isForce: Int,
    private val url: String
) : Dialog(context, R.style.Dialog) {
    private var mBinding: PopupChooseSideBinding? = null

    init {
        mBinding = DataBindingUtil.inflate(
            LayoutInflater.from(context),
            R.layout.popup_choose_side,
            null,
            false
        )
        setContentView(mBinding?.root!!)
        window!!.attributes.width = DensityUtil.dip2px(258f)
        window!!.attributes.height = WindowManager.LayoutParams.WRAP_CONTENT

        mBinding?.tvContent?.text = content
        mBinding?.tvNewVersion?.text = "$newVersion"
        if (isForce == 1) {//强制更新
            setCancelable(false)
            mBinding?.line?.visibility = View.GONE
            mBinding?.llHandle?.visibility = View.GONE
            mBinding?.tvIsFouse?.visibility = View.VISIBLE
            mBinding?.btnUpdate2?.visibility = View.VISIBLE
        } else {
            setCancelable(true)
            mBinding?.line?.visibility = View.VISIBLE
            mBinding?.llHandle?.visibility = View.VISIBLE
            mBinding?.tvIsFouse?.visibility = View.GONE
            mBinding?.btnUpdate2?.visibility = View.GONE
        }

        mBinding?.btnUpdate?.setOnClickListener {
            if (mListenter != null) {
                mListenter?.invoke(isForce, url)
            }
            dismiss()
        }
        mBinding?.btnUpdate2?.setOnClickListener {
            if (mListenter != null) {
                mListenter?.invoke(isForce, url)
            }
            dismiss()
        }
        mBinding?.btnCancel?.setOnClickListener { dismiss() }
    }

    var mListenter: ((isForce: Int, url: String) -> Unit)? = null

    fun setListenter(listenter: ((isForce: Int, url: String) -> Unit)) {
        mListenter = listenter
    }
}